﻿using System;
using System.Collections;
using System.Web.UI;
using VitCode.Castle.DataAccess;

namespace VitCode.Web.UserControls
{
    public partial class GuidToShowObjCtl : UserControl
    {
        public string Text
        {
            get
            {
                if (txtObjId.Text.Length > 0 && txtObjId.Text.Length == 36)
                {
                    SetInfo();
                    return txtObjId.Text;
                }
                return "";
            }
            set { txtObjId.Text = value; }
        }

        public string ObjClassName
        {
            get { return txtObjClassName.Text; }
            set { txtObjClassName.Text = value; }
        }

        public string InitCurrUser
        {
            get { return hdnInitCurrUser.Value; }
            set
            {
                hdnInitCurrUser.Value = value;
                if (hdnInitCurrUser.Value == "true")
                {
                    txtObjId.Text = GenericContext.CurrUserId.ToString();
                    txtObjVal.Text = GenericContext.CurrUserName;
                }
            }
        }

        public Guid ObjId
        {
            get
            {
                if (txtObjId.Text.Length > 0 && txtObjId.Text.Length == 36)
                    return new Guid(txtObjId.Text);
                return Guid.Empty;
            }
            set { txtObjId.Text = value.ToString(); }
        }

        public bool Enabled
        {
            set { txtObjVal.Enabled = value; }
            get { return txtObjVal.Enabled; }
        }

        private void SetInfo()
        {
            var acc = new CastleAccess();
            if(txtObjId.Text.Trim().Length == 36)
            {
                IList objs = acc.GetObjectList(txtObjClassName.Text, "Id", txtObjId.Text);
                if (objs != null && objs.Count > 0)
                {
                    txtObjVal.Text = objs[0].ToString();
                }
            }
           
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SetInfo();
        }
    }
}
